package com.wanbatv.wangwangba;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.wanbatv.wangwangba.Pager.page1;
import com.wanbatv.wangwangba.Pager.page2;
import com.wanbatv.wangwangba.Pager.page3;
import com.wanbatv.wangwangba.Pager.page4;
import com.wanbatv.wangwangba.Pager.page5;
import com.wanbatv.wangwangba.util.AppUtil;
import com.wanbatv.wangwangba.util.DownloadTask;
import com.wanbatv.wangwangba.util.DownloadTaskReceiver;
import com.wanbatv.wangwangba.util.TVViewPager;
import com.wanbatv.wangwangba.utils.Product;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;


public class Login2Activity extends Activity {
    public static Login2Activity thelogin2;
    TVViewPager viewpager;
    ArrayList<View> viewContainter = new ArrayList<View>();
    private TextView tuijian_tv1;
    private TextView tuijian_tv2;
    private TextView tuijian_tv3;
    private TextView tuijian_tv4;
    private TextView tuijian_tv5;
    private RelativeLayout new_bg;
    int YEMA_NUM=1;
    private AlertDialog myDialog=null;
    private WangwangbaAdapter mAdpater=null;
    public static int DOGTV_NUM=0;
    public static  boolean IS_SHANG=true;
    float currentSize ;
    float normalSize ;

    DownloadTaskReceiver receiver;
    public static int DOGTV_NUM_XUANZE=0;
    public static int XIAOBAIKE_NUM_XUANZE=0;
    public static int WANGWANGSHOW_NUM_XUANZE=0;
    private JSONArray baike_arr=null;
    public static String baike_token=null;
    public static String shipin_url11=null;
    public static String shipin_url12=null;
    public static String shipin_url13=null;
    public static String shipin_url14=null;
    public static String shipin_url15=null;
    public static String shipin_url16=null;
    public static String shipin_url17=null;
    public static String shipin_url18=null;
    public static String dizhi11=null;
    public static String dizhi12=null;
    public static String dizhi13=null;
    public static String dizhi14=null;
    public static String dizhi15=null;
    public static String dizhi16=null;
    public static String dizhi17=null;
    public static String dizhi18=null;

    public static boolean url_wws11=false;
    public static boolean url_wws12=false;
    public static boolean url_wws13=false;
    public static boolean url_wws14=false;
    public static boolean url_wws15=false;
    public static boolean url_wws16=false;
    public static boolean url_wws17=false;
    public static boolean url_wws18=false;
    public void huoqu_url1(String url){
        shipin_url11=url;
    }
    public void huoqu_url2(String url){
        shipin_url12=url;
    }
    public void huoqu_url3(String url){
        shipin_url13=url;
    }
    public void huoqu_url4(String url){
        shipin_url14=url;
    }
    public void huoqu_url5(String url){
        shipin_url15=url;
    }

    public void huoqu_url6(String url){
        shipin_url16=url;
    }
    public void huoqu_url7(String url){
        shipin_url17=url;
    }
    public void huoqu_url8(String url){
        shipin_url18=url;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        updataAPK("http://121.201.14.248:8061/dogtv/getversion?flavor=" + Product.flavor + "&type=release&productname=wangwangba&platform=android",
                AppUtil.converVersionNameToInt(AppUtil.getAppVersionName(this)));
        viewpager=(TVViewPager)findViewById(R.id.viewpager);
        viewpager.setOffscreenPageLimit(4);
        viewpager.setPageMargin(-300);
        tuijian_tv1=(TextView)findViewById(R.id.tuijian_tv1);
        tuijian_tv2=(TextView)findViewById(R.id.tuijian_tv2);
        tuijian_tv3=(TextView)findViewById(R.id.tuijian_tv3);
        tuijian_tv4=(TextView)findViewById(R.id.tuijian_tv4);
        tuijian_tv5=(TextView)findViewById(R.id.tuijian_tv5);
        new_bg=(RelativeLayout)findViewById(R.id.new_bg);
         currentSize = getResources().getDimension(R.dimen.text_bianda);
         normalSize = getResources().getDimension(R.dimen.text_bianxiao);
        textsizechange();
        tuijian_tv1.setTextSize(currentSize);
        View view1 = LayoutInflater.from(this).inflate(R.layout.page1, null);
//        View view2 = LayoutInflater.from(this).inflate(R.layout.page2, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.page3, null);
        View view4 = LayoutInflater.from(this).inflate(R.layout.page4, null);
        View view5 = LayoutInflater.from(this).inflate(R.layout.page5, null);

        viewContainter.add(view1);
//        viewContainter.add(view2);
        viewContainter.add(view3);
        viewContainter.add(view4);
        viewContainter.add(view5);
        mAdpater=new WangwangbaAdapter();
        viewpager.setAdapter(mAdpater);
                viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        if (IS_SHANG == false) {
                            switch (position) {
                                case 0:
                                    if (YEMA_NUM == 2) {
                                        page1.tuijian_iv4.requestFocus();
                                        YEMA_NUM = 1;
                                    }
                                    break;
                                case 1:
                                    if (YEMA_NUM == 1) {
                                        page3.dogtv_iv1.requestFocus();
                                        YEMA_NUM = 2;
                                    }
                                    if (YEMA_NUM == 3) {
                                        page3.dogtv_iv6.requestFocus();
                                        YEMA_NUM = 2;
                                    }
                                    break;
                                case 2:
                                    if (YEMA_NUM == 2) {
                                        page4.baike_iv1.requestFocus();
                                        YEMA_NUM = 3;
                                    }
                                    if (YEMA_NUM == 4) {
                                        page4.baike_iv5.requestFocus();
                                        YEMA_NUM = 3;
                                    }
                                    break;
                                case 3:
                                    page5.wangwangshow_iv1.requestFocus();
                                    YEMA_NUM = 4;
                                    break;
                            }

                        }
                        switch (position) {
                            case 0:
                                textcolor();
                                tuijian_tv1.setTextColor(0xfffff799);
                                textsizechange();
                                tuijian_tv1.setTextSize(currentSize);
//                                new_bg.setBackgroundResource(R.drawable.loginbg_new1);
                                break;
                            case 1:
                                textcolor();
                                tuijian_tv3.setTextColor(0xfffff799);
                                textsizechange();
                                tuijian_tv3.setTextSize(currentSize);
//                                new_bg.setBackgroundResource(R.drawable.loginbg_new3);
                                break;
                            case 2:
                                textcolor();
                                tuijian_tv4.setTextColor(0xfffff799);
                                textsizechange();
                                tuijian_tv4.setTextSize(currentSize);
//                                new_bg.setBackgroundResource(R.drawable.loginbg_new4);
                                break;
                            case 3:
                                textcolor();
                                tuijian_tv5.setTextColor(0xfffff799);
                                textsizechange();
                                tuijian_tv5.setTextSize(currentSize);
//                                new_bg.setBackgroundResource(R.drawable.loginbg_new5);
                                break;

                        }
//                        mHandler.sendEmptyMessage(MSG_PLAY_PROGRESS);
                    }


                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
        RequestQueue rq = Volley.newRequestQueue(Login2Activity.this.getApplicationContext());
        JsonObjectRequest request = new JsonObjectRequest("http://121.201.7.173:8087/api/wangwangba/wikis/all", null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            JSONObject data=jsonObject.getJSONObject("data");
                            JSONArray arr=data.getJSONArray("list");
                            baike_arr=arr;
                            JSONObject faq=jsonObject.getJSONObject("faq");
                            baike_token=faq.getString("token");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq.add(request);

        RequestQueue rq1 = Volley.newRequestQueue(this.getApplicationContext());
        JsonObjectRequest request1 = new JsonObjectRequest("http://121.201.7.173:8087/api/wangwangba/shows/all", null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            JSONObject obj=jsonObject.getJSONObject("data");
                            JSONArray arr=obj.getJSONArray("list");
                            JSONObject obj1=arr.getJSONObject(2);
                            JSONObject obj2=arr.getJSONObject(3);
                            JSONObject obj3=arr.getJSONObject(4);
                            JSONObject obj4=arr.getJSONObject(5);
                            JSONObject obj5=arr.getJSONObject(6);
                            JSONObject obj6=arr.getJSONObject(7);
                            JSONObject obj7=arr.getJSONObject(0);
                            JSONObject obj8=arr.getJSONObject(1);

                            dizhi11=obj1.getString("content_name");
                            dizhi12=obj2.getString("content_name");
                            dizhi13=obj3.getString("content_name");
                            dizhi14=obj4.getString("content_name");
                            dizhi15=obj5.getString("content_name");
                            dizhi16=obj6.getString("content_name");
                            dizhi17=obj7.getString("content_name");
                            dizhi18=obj8.getString("content_name");
                            huoqu_url1(obj1.getString("token"));
                            huoqu_url2(obj2.getString("token"));
                            huoqu_url3(obj3.getString("token"));
                            huoqu_url4(obj4.getString("token"));
                            huoqu_url5(obj5.getString("token"));
                            huoqu_url6(obj6.getString("token"));
                            huoqu_url7(obj7.getString("token"));
                            huoqu_url8(obj8.getString("token"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq1.add(request1);

        page1.tuijian_iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WANGWANGSHOW_NUM_XUANZE=0;
                all_false();
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page1.tuijian_iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baike_token=null;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
        page1.tuijian_iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WANGWANGSHOW_NUM_XUANZE=6;
                all_false();
                url_wws17=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page1.tuijian_iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WANGWANGSHOW_NUM_XUANZE=7;
                all_false();
                url_wws18=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page4.baike_iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XIAOBAIKE_NUM_XUANZE=0;
                try {
                    JSONObject obj=baike_arr.getJSONObject(XIAOBAIKE_NUM_XUANZE);
                    baike_token=obj.getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
        page4.baike_iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XIAOBAIKE_NUM_XUANZE=1;
                try {
                    JSONObject obj=baike_arr.getJSONObject(XIAOBAIKE_NUM_XUANZE);
                    baike_token=obj.getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
        page4.baike_iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XIAOBAIKE_NUM_XUANZE=2;
                try {
                    JSONObject obj=baike_arr.getJSONObject(XIAOBAIKE_NUM_XUANZE);
                    baike_token=obj.getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
        page4.baike_iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XIAOBAIKE_NUM_XUANZE=3;
                try {
                    JSONObject obj=baike_arr.getJSONObject(XIAOBAIKE_NUM_XUANZE);
                    baike_token=obj.getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
        page4.baike_iv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XIAOBAIKE_NUM_XUANZE=4;
                try {
                    JSONObject obj=baike_arr.getJSONObject(XIAOBAIKE_NUM_XUANZE);
                    baike_token=obj.getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
        page3.dogtv_iv6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baike_token=null;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, XiaobaikeActivity.class);
                startActivity(it);
            }
        });
//        page2.chengzhang_iv1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent it = new Intent();
//                it.setClass(Login2Activity.this, ChengzhangrijiActivity.class);
//                startActivity(it);
//            }
//        });

        page5.wangwangshow_iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                all_false();
                url_wws11=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page5.wangwangshow_iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                all_false();
                url_wws12=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page5.wangwangshow_iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                all_false();
                url_wws13=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page5.wangwangshow_iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                all_false();
                url_wws14=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page5.wangwangshow_iv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                all_false();
                url_wws15=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page5.wangwangshow_iv6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                all_false();
                url_wws16=true;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);
            }
        });
        page3.dogtv_iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DOGTV_NUM=1;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, MainActivity.class);
                startActivity(it);
            }
        });
        page3.dogtv_iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DOGTV_NUM=2;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, MainActivity.class);
                startActivity(it);
            }
        });
        page3.dogtv_iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DOGTV_NUM=3;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, MainActivity.class);
                startActivity(it);
            }
        });
        page3.dogtv_iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DOGTV_NUM=4;
                Intent it = new Intent();
                it.setClass(Login2Activity.this, MainActivity.class);
                startActivity(it);
            }
        });
        page3.dogtv_iv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WANGWANGSHOW_NUM_XUANZE=0;
                all_false();
                Intent it = new Intent();
                it.setClass(Login2Activity.this, ShowBofangActivity.class);
                startActivity(it);

            }
        });

        tuijian_tv1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    viewpager.setCurrentItem(0, true);
                    tuijian_tv1.setBackgroundResource(R.drawable.main_title_background_focused);
                    IS_SHANG = true;

                } else {
                    tuijian_tv1.setBackgroundDrawable(null);

                }
            }
        });
        tuijian_tv3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    viewpager.setCurrentItem(1, true);
                    tuijian_tv3.setBackgroundResource(R.drawable.main_title_background_focused);
                    IS_SHANG=true;

                }else {
                    tuijian_tv3.setBackgroundDrawable(null);


                }
            }
        });
        tuijian_tv4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    viewpager.setCurrentItem(2,true);
                    tuijian_tv4.setBackgroundResource(R.drawable.main_title_background_focused);
                    IS_SHANG=true;

                }else {
                    tuijian_tv4.setBackgroundDrawable(null);

                }
            }
        });
        tuijian_tv5.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    viewpager.setCurrentItem(3,true);
                    tuijian_tv5.setBackgroundResource(R.drawable.main_title_background_focused);
                    IS_SHANG=true;
                }else {
                    tuijian_tv5.setBackgroundDrawable(null);


                }
            }
        });
//        page1 p1=new page1(this);
//        page2 p2=new page2(this);
//        page3 p3=new page3(this);
//        page4 p4=new page4(this);
//        page5 p5=new page5(this);

    }
//    private void creatAlertDialog6() {
//
//        myDialog = new AlertDialog.Builder(this).create();
//
//        myDialog.show();
//
//        myDialog.getWindow().setContentView(R.layout.diolag_faq);
//
//        myDialog.getWindow()
//                .findViewById(R.id.dia_close)
//                .setOnClickListener(new View.OnClickListener() {
//
//                    @Override
//                    public void onClick(View v) {
//
//                        myDialog.dismiss();
//                    }
//
//                });
//
//    }

    public void textcolor(){
        tuijian_tv1.setTextColor(0xffffffff);
        tuijian_tv2.setTextColor(0xffffffff);
        tuijian_tv3.setTextColor(0xffffffff);
        tuijian_tv4.setTextColor(0xffffffff);
        tuijian_tv5.setTextColor(0xffffffff);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private class WangwangbaAdapter extends PagerAdapter{

            public int getCount() {
                return viewContainter.size();
            }

            //滑动切换的时候销毁当前的组件
            @Override
            public void destroyItem(ViewGroup container, int position,
            Object object) {
                ((ViewPager) container).removeView(viewContainter.get(position));
            }

            //每次滑动的时候生成的组件
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ((ViewPager) container).addView(viewContainter.get(position));
                return viewContainter.get(position);
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public int getItemPosition(Object object) {
                return super.getItemPosition(object);
            }
//
//            @Override
//            public float getPageWidth(int position) {
////                if(position==3){
////                    return 1;
////                }else {
//                    float num = 0.88f;
//                    return num;
////                }
//            }

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_DPAD_UP){
            if(page1.IS_F1){
               tuijian_tv1.requestFocus();
            }
            if(page3.IS_F3){
                tuijian_tv3.requestFocus();
            }
            if(page4.IS_F4){
                tuijian_tv4.requestFocus();
            }
            if(page5.IS_F5){
                tuijian_tv5.requestFocus();
            }
        }
        if(keyCode==KeyEvent.KEYCODE_BACK){
            creatAlertDialog_tuichu();
        }
        return super.onKeyDown(keyCode, event);
    }
    private void textsizechange(){
        tuijian_tv1.setTextSize(normalSize);
        tuijian_tv3.setTextSize(normalSize);
        tuijian_tv4.setTextSize(normalSize);
        tuijian_tv5.setTextSize(normalSize);
    }
    /**
     * 下载app具体操作
     *
     * @param appId
     * @param downloadUrl
     * @param appName
     * @param appDesc
     * @param appSize
     */
    public void download(String appId, String downloadUrl, String appName,
                         String appDesc, long appSize) {
        // 初始化DownloadTask实例
        DownloadTask aDownloadTask = new DownloadTask(appId,
                Uri.parse(downloadUrl), appName, appDesc, appSize);
        // 实例化DownloadManager.Request
        DownloadManager.Request request = new DownloadManager.Request(
                aDownloadTask.getDownloadURI());
        // 系统下载路径
        request.setDestinationInExternalFilesDir(Login2Activity.this,
                Environment.DIRECTORY_DOWNLOADS, appName + ".apk");

        // request.setShowRunningNotification(true);
        // 向队列加载一个新的下载项
        DownloadManager downloadManager = ((DownloadManager) Login2Activity.this
                .getSystemService(DOWNLOAD_SERVICE));
        long downloadId = downloadManager.enqueue(request);
        // 保存downloadId
        aDownloadTask.setDownloadId(downloadId);
        // 保存DownloadTask对象
        // tasks.add(aDownloadTask);
        // 过滤器 当前下载任务完成
        IntentFilter filter = new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        // 注册并监听过滤器
        Login2Activity.this.registerReceiver(receiver, filter);

    }
    private void updataAPK(String url,final int currVersion){
        receiver=new DownloadTaskReceiver();
        JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject obj) {
                        try {
                            JSONObject response=obj.getJSONObject("data");

                            // 版本是否更新 0否 1是
                            String versionName = response.getString("versionName");
                            if (AppUtil.converVersionNameToInt(versionName) > currVersion) {
                                // 获取apk下载地址
                                final String apkUpdateUrl = response
                                        .getString("apk");
                                // 是否强制更新 0否 1是
                                int isForceUpdate = response
                                        .getInt("isForce");

                                AlertDialog.Builder b2 = new AlertDialog.Builder(
                                        Login2Activity.this)
                                        .setTitle("提示")
                                        .setMessage("升级到最新版本")
                                        .setPositiveButton(
                                                "确定",
                                                new AlertDialog.OnClickListener() {
                                                    public void onClick(
                                                            DialogInterface dialog,
                                                            int which) {
                                                        Toast.makeText(
                                                                Login2Activity.this,
                                                                "开始下载！",
                                                                Toast.LENGTH_LONG)
                                                                .show();
                                                        download("",
                                                                apkUpdateUrl,
                                                                "wangwangba",
                                                                "", 0);
                                                    }
                                                });

                                if (isForceUpdate == 0) {
                                    b2.setNegativeButton("取消",
                                            new AlertDialog.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    dialog.dismiss();
                                                }
                                            });
                                    b2.setCancelable(true);
                                } else {
                                    b2.setCancelable(false);
                                }
                                b2.create();
                                b2.show();
                            }

                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Login2Activity.this, "更新访问失败！",
                        Toast.LENGTH_LONG).show();
            }
        });

        // 将 请求放入 请求队列
        AppUtil.getInstance(this).getRequestQueue().add(mJsonObjectRequest);
    }

    public static void all_false(){
        url_wws11=false;
        url_wws12=false;
        url_wws13=false;
        url_wws14=false;
        url_wws15=false;
        url_wws16=false;
        url_wws17=false;
        url_wws18=false;
    }

    private void creatAlertDialog_tuichu() {

        myDialog = new AlertDialog.Builder(this).create();

        myDialog.show();

        myDialog.getWindow().setContentView(R.layout.diolag_tuichu);

        myDialog.getWindow()
                .findViewById(R.id.no_iv)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        myDialog.dismiss();

                    }

                });
        myDialog.getWindow()
                .findViewById(R.id.yes_iv)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                            finish();

                    }

                });
    }
}
